let loadingObj = {}
const httpServer = (url, method, type, data, loading) => {
	var interfaceUrl = 'https://tbpapi.jinghushi.com/';
	// var interfaceUrl = 'https://bpapi.jinghushi.com/';
	var token = "";
	var mid = "";
	var b = '';
	var ver = uni.getStorageSync('ver');
	var lng = uni.getStorageSync('x');
	var lat = uni.getStorageSync('y');
	// #ifdef MP-WEIXIN
	b = 'WEIXIN';
	// #endif
	// #ifdef MP-ALIPAY
	b = 'ALI';
	// #endif
	// #ifdef H5
	b = 'H5';
	// #endif
	// #ifdef APP-PLUS
	b = 'APP';
	// #endif
	try {
		const value = uni.getStorageSync('token');
		const midval = uni.getStorageSync('mid');
		if (value) {
			token = value
		}
		mid = midval
	} catch (e) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('mid');
	}
	if (type == 'bonus') {
		interfaceUrl += 'bonus.php/'
	}
	if (type == 'creditshop') {
		interfaceUrl += 'creditshop.php/'
	}
	if (type == 'commission') {
		interfaceUrl += 'commission.php/'
	}
	if (type == 'coupon') {
		interfaceUrl += 'coupon.php/'
	}
	if (type == 'lottery') {
		interfaceUrl += 'lottery.php/'
	}
	if (type == 'member') {
		interfaceUrl += 'member.php/'
	}
	if (type == 'order') {
		interfaceUrl += 'order.php/'
	}
	if (type == 'salemsg') {
		interfaceUrl += 'salemsg.php/'
	}
	if (type == 'sale') {
		interfaceUrl += 'sale.php/'
	}
	if (type == 'shop') {
		interfaceUrl += 'shop.php/'
	}
	if (type == 'zhibo') {
		interfaceUrl += 'zhibo.php/'
	}
	if (type == 'group') {
		interfaceUrl += 'group.php/'
	}
	if (type == 'recmd') {
		interfaceUrl += 'recmd.php/'
	}
	if (type == 'saleadv') {
		interfaceUrl += 'saleadv.php/'
	}
	if (type == 'jjgy') {
		// interfaceUrl='http://api.test.jiujiubaopin.com/';
	}
	if (type == 'jd') {
		interfaceUrl += 'jd.php/'
	}
	if (type == 'supplier') {
		interfaceUrl += 'supplier.php/'
	}
	if (type == 'kf') {
		interfaceUrl += 'kf.php/'
	}
	if (type == 'douyin') {
		interfaceUrl += 'douyin.php/'
	}
	if (type == 'duoduo') {
		interfaceUrl += 'duoduo.php/'
	}
	if (type == 'api') {
		interfaceUrl += 'api/'
	}
	if (type == 'regional') {
		interfaceUrl += 'regional.php/'
	}
	if (type == 'ai') {
		interfaceUrl += 'ai.php/'
	}
	if (type == 'pintuan') {
		interfaceUrl += 'pintuan.php/'
	}
	// 拼团类型
	if (type == 'group_buy') {
		interfaceUrl += 'group_buy.php/'
	}
	if (type == 'pt') {
		interfaceUrl += 'pt.php/'
	}


	let httpDefaultOpts = {
		url: interfaceUrl + url,
		data: data,
		method: method,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'token': token,
			'mid': mid,
			'ver': ver,
			'b': b,
			'lat': lat,
			'lng': lng,
		},
		dataType: 'json',
	}
	if (loading == 1) {
		if (loadingObj && loadingObj[httpDefaultOpts.url] == 1) {
			return Promise.reject(new Error('重复请求'))
		}
		loadingObj[httpDefaultOpts.url] = 1
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				if (res.data.code != 200 && res.data.code != 1002 && res.data.code != 300001 && res.data
					.code != 0 && res.data.code != 1000) {
					if (res.data.data) {
						uni.showToast({
							title: res.data.data,
							icon: "none",
							duration: 3000
						})
					}
				}
				if (res.data.code == 1002) {
					let ver = uni.getStorageSync('ver');
					uni.clearStorage();
					uni.setStorageSync('ver', ver);
					uni.setStorageSync('mid', res.data.data.mid);
					uni.reLaunch({
						url: '/pages/public/login?mid=' + res.data.data.mid
					})
				}
				if (res.data.code == 300001) {
					uni.redirectTo({
						url: res.data.data
					})
				}
				resolve(res)
			}
		).catch(
			(response) => {
				reject(response)
			}
		).finally(() => {
			if (loading == 1 && loadingObj && loadingObj[httpDefaultOpts.url] === 1) {
				delete loadingObj[httpDefaultOpts.url]
			}
		})
	})
	return promise
}

export default httpServer